package userModule.controller;

import userModule.service.UserService;
import vo.Library_User;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.UUID;

@WebServlet(name = "ModifyUserServlet", value = "/modifyUser")
public class ModifyUserServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");

        UserService us = new UserService();

        //1.获取注册信息
        String userId = request.getParameter("userId");
        String username = request.getParameter("usernamenew");
        String password = request.getParameter("password");
        String role = request.getParameter("role");
        String major = request.getParameter("major");
        String phoneNum = request.getParameter("phoneNum");

        //2.根据修改的信息封装一个User的实体类
        Library_User user = new Library_User(
                userId,
                username,
                password,
                role,
                major,
                phoneNum
        );

        System.out.println(user);

        boolean isModifySuccess = us.modifyUser(user);
        if(isModifySuccess){
            request.setAttribute("info","修改成功");
            request.setAttribute("user",user);
        }else{
            request.setAttribute("info","修改失败");
        }
        request.getRequestDispatcher("/jsp/modifyUser.jsp").forward(request,response);
    }
}
